// 服务器推送消息给浏览器
var app=require('express')();
var server=require("http").Server(app);
// 建立的通道一直在 ws:// http://
var WebSocket = require('ws');
var wss = new WebSocket.Server({port:8080});
wss.on('connection',function(ws){
    console.log('server: receive connection');
    // 客户端发送消息 
    ws.on('message',function(msg){
        console.log('server :receive connection %s',msg)
    })
    // 服务器有主动和客户端连接的功能
    ws.send('server world');
    setTimeout(() => {
        ws.send('hello world');
    }, 2000);
})

// url / get 
app.get('/',function(req,res){
    res.sendFile(__dirname+'/index.html');
})
app.listen(3000);
